// ---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit2.h"
#include "sudoku_board.h"

// ---------------------------------------------------------------------------
#pragma package(smart_init)
TMyEdit *MyEdit[9][9]; // define (grid) array of special Edits

// ---------------------------------------------------------------------------
__fastcall TMyEdit::TMyEdit(TComponent* Owner)
	: TEdit(Owner) // have constructor set common attributes
{
	AutoSize = false;
	Height = 52;
	Width = 52;
	Alignment = taCenter;
	Font->Size = 30;
	setUp = false;
	MaxLength = 1;
	NumbersOnly = true;
	ReadOnly = false;
	ShowHint = false;
	Font->Color = Form3->ColorDialog1->Color;
	for (int i = 0; i < 10; i++)
	{
		hint[i] = true;
	}


	OnMouseEnter = MyMouseEnter;
	OnChange = MyEditChange;
	OnMouseDown = MyMouseDown;
	// OnMouseUp=MyMouseUp;
}


//-----------------------------------------------------------------------------
void __fastcall TMyEdit::MyEditChange(TObject* Sender)
{
	int value1 = 0;
	if (MyEdit[col][row]->Text != "")
	{

		value1 = StrToInt(MyEdit[col][row]->Text);
		//Form3->setoldValue(value1);
		//check the for a duplicates.
		if(checkRow(row, value1) || checkCol(col, value1) || checkQuad(row, col, value1))
		{
			MyEdit[col][row]->Text = "";
		}
	}

	// if button1 caption is "Play" then we are in SETUP mode.
	if (Form3->Button1->Caption == "Play")
	{
		// check if current edit box has a number in it
		if (MyEdit[col][row]->Text != "")
		{
			// remove the current number from all other boxes as candidates
			MyEdit[col][row]->setsetUp(true);

		}
		else
		{   // add the current number to all other boxes as candidates
			MyEdit[col][row]->setsetUp(false);

		}
	}
	// if button1 caption is "Reset" then we are in Play mode.
	else if (Form3->Button1->Caption == "Reset")
	{
				MyEdit[col][row]->Font->Color = Form3->ColorDialog2->Color;

	}
//		ShowMessage(Form3->getoldValue());


}




//-----------------------------------------------------------------------------
bool TMyEdit::checkRow(int row, int value1)
{
	int value2;
	for (int i = 0; i < 9; i++)
	{
		if (MyEdit[i][row]->Text != "")
		{
			value2 = StrToInt(MyEdit[i][row]->Text);
			if (value1 == value2 && i != col)
			{
				MyEdit[i][row]->Color = clRed;
				MyEdit[col][row]->Color = clRed;
				ShowMessage("Duplicate Values in Red");
				MyEdit[i][row]->Color = clWindow;
				MyEdit[col][row]->Color = clWindow;
				return true;
			}


		}
	}
	return false;
}


//-----------------------------------------------------------------------------
bool TMyEdit::checkCol(int col, int value1)
{
	int value2;
	for (int i = 0; i < 9; i++)
	{
		if (MyEdit[col][i]->Text != "")
		{
			value2 = StrToInt(MyEdit[col][i]->Text);
			if (value1 == value2 && i != row)
			{
				MyEdit[col][i]->Color = clRed;
				MyEdit[col][row]->Color = clRed;
				ShowMessage("Duplicate Values in Red");
				MyEdit[col][i]->Color = clWindow;
				MyEdit[col][row]->Color = clWindow;
				return true;
			}



		}
	}
	return false;
}

//-----------------------------------------------------------------------------
bool TMyEdit::checkQuad(int row, int col, int value1)
{
	int value2;
	for (int i = ((col / 3) * 3); i < ((col / 3) * 3 + 3); i++)
			{
				for (int j = ((row / 3) * 3); j < ((row / 3) * 3 + 3); j++)
				{
					if (MyEdit[i][j]->Text != "")
					{
						value2 = StrToInt(MyEdit[i][j]->Text);
						if (value1 == value2 && i != col && j != row)
						{

							MyEdit[i][j]->Color = clRed;
							MyEdit[col][row]->Color = clRed;
							ShowMessage("Duplicate Values in Red");
							MyEdit[i][j]->Color = clWindow;
							MyEdit[col][row]->Color = clWindow;
							return true;
							MyEdit[col][row]->Text = "";

						}
					}
				}
			}
	return false;
}


// -------------------------------------------------------------------------

void TMyEdit::setLocation(int col, int row)
{
	this->col = col;
	this->row = row;
}

// -------------------------------------------------------------------------
bool TMyEdit::getsetUp()
{
	return setUp;
}

void TMyEdit::setsetUp(bool toggle)
{
	setUp = toggle;
}

// -------------------------------------------------------------------------
void __fastcall TMyEdit::MyMouseDown(TObject *Sender, TMouseButton Button,
	TShiftState Shift, int X, int Y)
{
//		if (MyEdit[col][row]->Text != "")
//		{
//			Form3->setoldValue(StrToInt(MyEdit[col][row]->Text));
//  	}
}
//-----------------------------------------------------------------------------
void __fastcall TMyEdit::MyMouseEnter(TObject *Sender)
{
	if (MyEdit[col][row]->Text == "" && MyEdit[col][row]->ReadOnly == false)
	{

		UnicodeString hintString;
		bool hint[10];
		for (int i = 0; i < 10; i++)
		{
			hint[i] = true;
		}

		hintString = "Hint:";
		for (int i = 0; i < 9; i++)
		{
			if (MyEdit[i][row]->Text != "")
			{
				hint[StrToInt(MyEdit[i][row]->Text)] = false;
			}

		}
		for (int i = 0; i < 9; i++)
		{
			if (MyEdit[col][i]->Text != "")
			{
				hint[StrToInt(MyEdit[col][i]->Text)] = false;
			}
		}
		for (int i = ((col / 3) * 3); i < ((col / 3) * 3 + 3); i++)
		{
			for (int j = ((row / 3) * 3); j < ((row / 3) * 3 + 3); j++)
			{
				if (MyEdit[i][j]->Text != "")
				{
					hint[StrToInt(MyEdit[i][j]->Text)] = false;
				}
			}
		}
		for (int i = 1; i < 10; i++)
		{
			if (hint[i])
			{
				hintString = hintString + " " + IntToStr(i);
			}
		}

		if (hintString == "Hint:")
		{
			MyEdit[col][row]->Hint = "There are no hints for this box, try changing another box";
		}
		else
		{
			MyEdit[col][row]->Hint = hintString;
		}
	}
}




//------------------------------------------------------------------------------
void TMyEdit::setAllNums()
{
	for (int i = 1; i < 10; i++)
	{
		hint[i] = true;
	}

}


